import * as types from "./action-types"
import axios from "axios"
import { fromJS } from "immutable"


//同步
export const setScroll = (data) => ({ type: types.SET_HOME_SCROLL, data });

//异步
const getHomeTopic = (data) => ({ type: types.GET_HOME_TOPIC, data });
const getHomeList = (data) => ({ type: types.GET_HOME_LIST, data });
const loadMoreData = (data) => ({ type: types.GET_LOAD_MORE, data: fromJS(data) });

export const getHomeData = () => {
    return (dispatch) => {
        axios.all([
            axios.get("/api/homeList.json"),
            axios.get("/api/homeTopicList.json")
        ]).then(
            axios.spread((res1, res2) => {
                dispatch(getHomeTopic(res2.data.data))
                dispatch(getHomeList(res1.data.data))
            })
        )
    }
}

export const loadMore = (pages) => {
    return (dispatch) => {
        console.log("action-pages: ", pages);
        axios.get("/api/homeList2.json?page=" + pages).then(res => {
            dispatch(loadMoreData(res.data.data))
        })
    }
}


